package com.fqgj.sentry.biz.pc;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.biz.pc.vo.event.EventVo;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.controller.pc.request.AddEventVo;
import com.fqgj.sentry.controller.pc.request.EventPageVo;
import com.fqgj.sentry.controller.pc.request.UpdateEventVo;
import com.fqgj.sentry.policy.client.EventService;
import com.fqgj.sentry.policy.domain.Event;
import com.fqgj.sentry.policy.entity.PcEventEntity;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zwh @yuntu-inc.com
 *
 * @description
 * @create 2017/7/26 0026 下午 8:31
 */
@Component
public class EventBusiness {
    @Autowired
    private EventService eventService;

    public Boolean saveEvent(Long merchantId, AddEventVo addEventVo) {
        PcEventEntity pcEventEntity = new PcEventEntity()
                .setCode(addEventVo.getCode())
                .setName(addEventVo.getName())
                .setMerchantId(merchantId)
                .setInfo(addEventVo.getInfo());
        return null != eventService.saveEvent(pcEventEntity).getId();
    }

    public Boolean deleteEvent(Long id) {
        return eventService.deleteEvent(id) > 0;
    }

    public Boolean updateEvent(UpdateEventVo updateEventVo) {
        Event event = new Event();
        BeanUtils.copyProperties(updateEventVo, event);
        return eventService.updateEvent(event) > 0;
    }

    public ApiPage listEvent(Long merchantId, EventPageVo eventPageVo) {
        ApiPage apiPage = new ApiPage()
                .setPageNum(eventPageVo.getPageNum())
                .setPageSize(eventPageVo.getPageSize());

        List<PcEventEntity> eventEntityList = eventService.listEvent(
                merchantId, eventPageVo.getCode(), eventPageVo.getName(), apiPage);
        List<EventVo> eventVoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(eventEntityList)) {
            for (PcEventEntity pcEventEntity : eventEntityList) {
                EventVo eventVo = new EventVo()
                        .setId(pcEventEntity.getId())
                        .setCode(pcEventEntity.getCode())
                        .setName(pcEventEntity.getName())
                        .setMerchantId(pcEventEntity.getMerchantId())
                        .setInfo(pcEventEntity.getInfo());
                eventVoList.add(eventVo);
            }
        }
        apiPage.setData(eventVoList);
        return apiPage;
    }

    /**
     * 该商户下  所有事件
     *
     * @param merchantId
     * @return
     */
    public List<EventVo> comboEvent(Long merchantId) {
        List<PcEventEntity> pcEventEntityList = eventService.comboEvent(merchantId);
        List<EventVo> eventVoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(pcEventEntityList)) {
            for (PcEventEntity pcEventEntity : pcEventEntityList) {
                EventVo eventVo = new EventVo()
                        .setId(pcEventEntity.getId())
                        .setCode(pcEventEntity.getCode())
                        .setName(pcEventEntity.getName())
                        .setMerchantId(pcEventEntity.getMerchantId())
                        .setInfo(pcEventEntity.getInfo());
                eventVoList.add(eventVo);
            }
        }
        return eventVoList;
    }


}
